<?php

/**
 * CollectionResource form.
 *
 * @package    travide
 * @subpackage form
 * @author     Travide Project Group
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CollectionResourceForm extends BaseCollectionResourceForm {
    public function configure() {

        /**
         * Set the widget formatter to InlineTable
         */
        $this->getWidgetSchema()->addFormFormatter('InlineTable', new InlineTableFormSchemaFormatter($this->getWidgetSchema()));
        $this->getWidgetSchema()->setFormFormatterName('InlineTable');
        /**
         * Set the widget for resource data
         */
        $this->setWidget('resourceLink', new sfWidgetFormInputFile());
        $this->setValidator('resourceLink', new sfValidatorFile(array('path' => CollectionResourceTable::getResourceUploadDir() )));

        $w = $this->getWidget('resourceName');
        $w->setLabel('Name');


        $w = $this->getWidget('resourceLink');
        $w->setLabel('Resource Link');

        if (!$this->isNew) {
            /**
             * Add the Delete Resource Widget - resourceDelete
             * Allows user to select to delete the property on edit
             */
            $w = new sfWidgetFormInputCheckbox();
            $w->setLabel('Delete');
            $this->setWidget('resourceDelete', $w);
            $this->setValidator('resourceDelete', new sfValidatorBoolean());

            /**
             * Don't permit the user to alter the file uploaded
             * only deletes are permitted
             */
            unset($this['resourceLink']);
        }


        /**
         * Remove thisCollectionUin - as we don't want this to show up on the
         * new or edit forms for Collections - it will be set automatically
         */
        unset($this['collectionUin'],$this['resourceData'],
                $this['resourceSize'],$this['originalFilename'],
                $this['requiresIngest'],$this['deleted_at'],$this['base64Encoded']);
    }

    protected function processUploadedFile($field, $filename = null, $values = null) {
        /**
         * If resourceLink is being processed as a file then
         * will need to to set the metadata for the file in the database
         * and make for ingestion later.
         */
        if ($field == 'resourceLink') {
            $obj = $this->getObject();
            $obj->setResourceSize($values[$field]->getSize());
            $obj->setOriginalFilename($values[$field]->getOriginalName());
            $obj->setRequiresIngest(true);
        }
        return parent::processUploadedFile($field, $filename, $values);
    }
}
